package cn.fintecher.pangolin.service.business.scheduled;

import cn.fintecher.pangolin.common.enums.AssistStatus;
import cn.fintecher.pangolin.common.enums.BaseSelect;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.mysql.AssistCase;
import cn.fintecher.pangolin.entity.mysql.CaseInfo;
import cn.fintecher.pangolin.entity.mysql.QAssistCase;
import cn.fintecher.pangolin.entity.mysql.QCaseInfo;
import cn.fintecher.pangolin.service.business.repository.jparepository.AssistCaseRepository;
import cn.fintecher.pangolin.service.business.repository.jparepository.CaseInfoRepository;
import com.google.common.collect.Lists;
import com.querydsl.core.BooleanBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author wangxiangdong
 * @Description 外访过期检查
 * @Date 2019/10/14 11:07
 **/
@EnableScheduling
@Component
@Slf4j
public class VisitCaseExpireScheduled {

    @Autowired
    private AssistCaseRepository assistCaseRepository;

    @Autowired
    private CaseInfoRepository caseInfoRepository;

    @Scheduled(cron = "0 0 1 * * ?")
    @Transactional
    public void visitCaseExpireValidate() {
        log.info("外访调度任务开始:{}",ZWDateUtil.getFormatNowDateTime("yyyy-MM-dd HH:mm:ss"));
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(QAssistCase.assistCase.assistStatus.in(AssistStatus.ASSIST_WAIT_ASSIGN,AssistStatus.ASSIST_COLLECTING))
        .and(QAssistCase.assistCase.visitTimeEnd.before(ZWDateUtil.getNowDate()));
        List<AssistCase> assistCaseList = Lists.newArrayList(assistCaseRepository.findAll(booleanBuilder).iterator());
        Map<String,CaseInfo> caseInfoMap ;
        if(!assistCaseList.isEmpty()){
            List<CaseInfo> caseInfoList = Lists.newArrayList(caseInfoRepository.findAll(QCaseInfo.caseInfo.id.in(assistCaseList.stream().map(AssistCase::getCaseId).collect(Collectors.toList()))));
            caseInfoMap = caseInfoList.stream().collect(Collectors.toMap(CaseInfo::getId,caseInfo -> caseInfo));
        }else{
            log.info("外访调度任务结束，没有到期的协催案件");
            return;
        }
        List<CaseInfo> caseInfoList =  new ArrayList<>();
        assistCaseList.forEach(assistCase -> {
            assistCase.setAssistStatus(AssistStatus.ASSIST_CANCEL);
            CaseInfo caseInfo = caseInfoMap.get(assistCase.getCaseId());
            caseInfo.setIsAssist(BaseSelect.NO);
            caseInfoList.add(caseInfo);
        });
        assistCaseRepository.saveAll(assistCaseList);
        caseInfoRepository.saveAll(caseInfoList);
        log.info("外访调度任务结束:{}",ZWDateUtil.getFormatNowDateTime("yyyy-MM-dd HH:mm:ss"));
    }
}
